{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3 python的类属性和类方法\n",
    "\n",
    "- 3.1 review\n",
    "- 3.2 class methods\n",
    "- 3.3 type transform\n",
    "- 3.4 first program rule\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## 3.1 review\n",
    "首先复习一下前面，到此位置，我们对python的语言特点总结为一下几条：\n",
    "- A program equal a worl, while the python programmer is a God.\n",
    "- The world consists of objects.\n",
    "- Object must be belong to an class\n",
    "- object should have its unique name if it want to survive in the world. \n",
    "\n",
    "另外，我们将所有的对象分为两类：\n",
    "- class对象。 这些对象是其它对象的模版，负责生成对应的对象。如int，float,bool,str等。\n",
    "- instance对象。服务于程序功能。\n",
    "\n",
    "\n",
    "\n",
    ">```\n",
    "介绍一个常用的函数对象input（还记得print函数对象吗？）:\n",
    "\n",
    "1 该函数的参数：是一个字符串对象，将会显示在屏幕上，提示用户需要的输入类型。\n",
    "2 功能。显示提示信息，接受用户的键盘输入，直到用户按下回车键。\n",
    "3 执行结束后，生成一个字符串对象，该对象的内容为用户本次的所有输入。\n",
    "```\n",
    "\n",
    "参看下面的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pls input a number\n",
      "123\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "i = input('pls input a number\\n')\n",
    "print(type(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2 几个对象的功能细谈\n",
    "### 3.2.1 概述\n",
    "dir函数的功能很强大，这是一个非常常用的函数，功能是显示对象的所有功能。可以看出，对象的每一个功能像一个函数，称之为方法（*method*）包含一下特点：\n",
    "- 具有一个可以表示该功能的名称。\n",
    "- 具有一个或多个参数，这些参数都是使用该功能时所需要的对象。\n",
    "- 使用对象功能的语法：对象名.方法名(参数1，参数2)。\n",
    "\n",
    "这里以int举例：\n",
    "可以看到，通过dir函数，列出了int对象所有的方法，并对其中的__add__方法做了调用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "print(dir(a))\n",
    "b = 4\n",
    "c = a.__add__(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.3 语法蜜糖（sytax sugar）\n",
    "每个语言都会有语法蜜糖：是指语言提供的程序员喜欢的语句，但这些句子在编译或解释之前，都会替换为其它相同功能的语句：预先编译阶段。\n",
    "原因：\n",
    "1. 程序员可以少写一些代码。\n",
    "2. 更重要的是代码更加直观，便于理解。\n",
    "\n",
    "参考下面的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的代码，在被执行前，解释器将其变成了下面的代码。这就是语法蜜糖。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "a = int(3)\n",
    "b = int(4)\n",
    "c = a.__add__(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.4 对象类型转换\n",
    "严格来说，python的对象具有不变性。和现实世界一样，当一个对象诞生后，它的类型不会发生变化，直到死亡。所以这里的说法不够严谨，包含两个层面的意思：\n",
    "1. 可以理解为python提供了几个内置函数，它们可以根据对象类型，生成新的类型的对象。 如int函数可以根据给定的字符串，生成一个新的与原对象无关的int对象。\n",
    "2. 其实，这几个函数并不是python预先内置的函数，它们的功能比你想想的要强大的多。他们分别是几个class对象的构造函数（后面我们会学习到）。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "<class 'int'>\n",
      "3.0\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "b = float(a)\n",
    "print(a)\n",
    "print(type(a))\n",
    "print(b)\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
